param(
    [string]$Summary = "",
    [string]$Motivation = "",
    [int]$TimeoutSeconds = 120
)

# Load Windows Forms
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Create form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Create Workflow"
$form.Size = New-Object System.Drawing.Size(600, 450)
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.MaximizeBox = $false
$form.MinimizeBox = $false

# Summary label
$lblSummary = New-Object System.Windows.Forms.Label
$lblSummary.Text = "Workflow Name:"
$lblSummary.Location = New-Object System.Drawing.Point(20, 20)
$lblSummary.Size = New-Object System.Drawing.Size(560, 20)
$form.Controls.Add($lblSummary)

# Summary textbox
$txtSummary = New-Object System.Windows.Forms.TextBox
$txtSummary.Location = New-Object System.Drawing.Point(20, 45)
$txtSummary.Size = New-Object System.Drawing.Size(560, 25)
$txtSummary.Text = $Summary
$form.Controls.Add($txtSummary)

# Motivation label
$lblMotivation = New-Object System.Windows.Forms.Label
$lblMotivation.Text = "Requirements:"
$lblMotivation.Location = New-Object System.Drawing.Point(20, 80)
$lblMotivation.Size = New-Object System.Drawing.Size(560, 20)
$form.Controls.Add($lblMotivation)

# Motivation textbox (multiline)
$txtMotivation = New-Object System.Windows.Forms.TextBox
$txtMotivation.Location = New-Object System.Drawing.Point(20, 105)
$txtMotivation.Size = New-Object System.Drawing.Size(560, 200)
$txtMotivation.Multiline = $true
$txtMotivation.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
$txtMotivation.AcceptsReturn = $true
$txtMotivation.Text = $Motivation
$form.Controls.Add($txtMotivation)

# Error label (hidden initially)
$lblError = New-Object System.Windows.Forms.Label
$lblError.Location = New-Object System.Drawing.Point(20, 315)
$lblError.Size = New-Object System.Drawing.Size(560, 20)
$lblError.ForeColor = [System.Drawing.Color]::Red
$lblError.Visible = $false
$form.Controls.Add($lblError)

# Create button
$btnCreate = New-Object System.Windows.Forms.Button
$btnCreate.Text = "Create"
$btnCreate.Location = New-Object System.Drawing.Point(380, 350)
$btnCreate.Size = New-Object System.Drawing.Size(90, 30)
$btnCreate.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Controls.Add($btnCreate)
$form.AcceptButton = $btnCreate

# Cancel button
$btnCancel = New-Object System.Windows.Forms.Button
$btnCancel.Text = "Cancel"
$btnCancel.Location = New-Object System.Drawing.Point(480, 350)
$btnCancel.Size = New-Object System.Drawing.Size(90, 30)
$btnCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.Controls.Add($btnCancel)
$form.CancelButton = $btnCancel

# Validation function
function Validate-Input {
    $lblError.Visible = $false

    if ([string]::IsNullOrWhiteSpace($txtSummary.Text)) {
        $lblError.Text = "Error: Workflow name is required"
        $lblError.Visible = $true
        return $false
    }

    if ([string]::IsNullOrWhiteSpace($txtMotivation.Text)) {
        $lblError.Text = "Error: Requirements are required"
        $lblError.Visible = $true
        return $false
    }

    return $true
}

# Generate change ID
function Generate-ChangeID {
    $timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
    return "WF-$timestamp"
}

# Detect complexity based on motivation length
function Detect-Complexity {
    param([string]$text)

    $length = $text.Length
    if ($length -gt 500) {
        return "complex"
    } elseif ($length -gt 200) {
        return "medium"
    } else {
        return "simple"
    }
}

# Create button click handler
$btnCreate.Add_Click({
    if (Validate-Input) {
        $form.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $form.Close()
    }
})

# Show form and get result
$result = $form.ShowDialog()

# Prepare output
$output = @{
    cancelled = $false
    error = ""
}

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    # User clicked Create
    $output.change_id = Generate-ChangeID
    $output.summary = $txtSummary.Text.Trim()
    $output.motivation = $txtMotivation.Text.Trim()
    $output.complexity = Detect-Complexity -text $output.motivation
} else {
    # User clicked Cancel
    $output.cancelled = $true
}

# Output as JSON
$output | ConvertTo-Json -Compress

# Cleanup
$form.Dispose()
